#include <cstdlib>
#include <sys/time.h>
#include "Chrono.hpp"

// Constructor
Chrono::Chrono(void)
{
  gap = (GLfloat)0.0f;
  activate = false;
  gettimeofday(&startTime, NULL);
}

// Accessors
GLfloat Chrono::GetTime(void) const{
  if(!activate)
    return gap;
  else
    {
      timeval currentTime;
			gettimeofday(&currentTime, NULL);
			return static_cast<GLfloat>(gap +(currentTime.tv_sec - startTime.tv_sec +(currentTime.tv_usec * 1e-6f - startTime.tv_usec * 1e-6f)));
		}
}

bool Chrono::IsActivate(void) const{
  return activate;
}

// Mutators
void Chrono::Reset(void){
  gap = (GLfloat)0.0f;
  gettimeofday(&startTime, NULL);
}

int Chrono::Desactivate(void){
  if(activate)
    {
      gap = this->GetTime();
      activate = false;
      return EXIT_SUCCESS;
    }
  else
    return EXIT_FAILURE;
}

int Chrono::Activate(void){
  if(!activate)
    {
      gettimeofday(&startTime, NULL);
      activate = true;
      return EXIT_SUCCESS;
    }
  else
    return EXIT_FAILURE;
}

